package com.malou.client.graficosestadisticos;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.event.ChartEvent;
import com.extjs.gxt.charts.client.event.ChartListener;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;
import com.malou.client.ventas.tienda.Tiendas;

public class PrendasVendidasxDiaPanel extends ContentPanel{
	
	private MalouConstants constants = GWT.create(MalouConstants.class);
	private final Chart chart = new Chart("gxt/chart/open-flash-chart.swf");
	Grid<BeanModel> detalle;
	DateField fecha;
	Button find;
	Status status;
	PrendasVendidasxDiaPanel singleton;
	public ComboBox<BeanModel> tienda;
	Label tiendaL;	
	
	@Override  
	protected void onRender(Element parent, int index) {  
		super.onRender(parent, index);
		tienda.setRawValue(Utilities.tienda);
	}
	
	public PrendasVendidasxDiaPanel(){
		singleton = this;
		setFrame(true);  
        setCollapsible(true);  
        setAnimCollapse(false);  
        
        setIcon(IconHelper.createPath("images/icons/table.png"));  
        setHeading(constants.graficosPrendasVendidasxDia());
        
                
        Tiendas.getComboTienda();
        
        tienda = new ComboBox<BeanModel>();       
	    tienda.setEditable(false);	    
	    tienda.setDisplayField("nombre");	    	    
	    tienda.setWidth(150);  
	    tienda.setStore(Tiendas.storeCombo);  
	    tienda.setTypeAhead(true);  
	    tienda.setTriggerAction(TriggerAction.ALL);
	    tienda.setValueField("nombre");
	    tienda.setUseQueryCache(false);
	    tienda.setAllowBlank(true);	    
	    
	    ToolBar toolBar = new ToolBar();
	    
	    tiendaL = new Label(constants.graficosTienda());
	    toolBar.add(tiendaL);
	    toolBar.add(tienda);
	    
        fecha = new DateField();        	
        fecha.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
        fecha.setAllowBlank(false);
        fecha.setValue(new Date());
        fecha.setMaxValue(new Date());      
        
        Label fechaL = new Label(constants.graficosfecha());
        fechaL.setStyleAttribute("paddingLeft", "20px");
        
        toolBar.add(fechaL);
        toolBar.add(fecha);        
        
        status = new Status();
	    status.setBusy(constants.cargando());
	    status.hide();    
	    status.setAutoWidth(true);
	    status.setStyleAttribute("paddingLeft", "20px");
	    toolBar.add(status);
        
        find = new Button(constants.find(), IconHelper.createPath("images/icons/find.png"));
        find.setStyleAttribute("paddingLeft", "20px");
        
        SelectionListener<ButtonEvent> sl_find;        
        sl_find = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {		
				if(validate()){
					buscar();				
				}
			}
        };
        find.addSelectionListener(sl_find);
        
        toolBar.add(find);
        
        setTopComponent(toolBar);
        
        setLayout(new BorderLayout());
        
        BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
        centerData.setMargins(new Margins(0));
        
        chart.setBorders(true);
                
        ChartModel model = new ChartModel(constants.graficosPrendasVendidasxDia(), "font-size: 14px; font-family: Verdana; text-align: center;");  
        model.setBackgroundColour("#fefefe");
        model.setLegend(new Legend(Position.TOP, true));  
        model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER); 
        
        BarChart bar = new BarChart(BarStyle.GLASS);          
        bar.setColour("#0000cc");
        
        BarDataProvider barProvider = new BarDataProvider("cantidad", "codigo");
        
        Graficos.getPrendasVendidasxDia();
        
        barProvider.bind(Graficos.storePrendasVendidasxDia);  
        bar.setDataProvider(barProvider);
        model.addChartConfig(bar);
        
        chart.setChartModel(model);
        
        ChartListener listener = new ChartListener() { 
        	public void chartClick(ChartEvent ce) {  
        		int row = ce.getChartConfig().getValues().indexOf(ce.getDataType()); 
        		GridSelectionModel<BeanModel> sm = detalle.getSelectionModel();
        		sm.select(row, true);
        	}
        };
        bar.addChartListener(listener);
        
        add(chart, centerData);
        
        BorderLayoutData southData = new BorderLayoutData(LayoutRegion.WEST, 363);  
        southData.setSplit(true);
       
        southData.setFloatable(true);  
        southData.setMargins(new Margins(0, 5, 0, 0));
        //grid
        
        RowNumberer r = new RowNumberer();
        
        List<ColumnConfig> columns = new ArrayList<ColumnConfig>();        
        columns.add(r);        
        
        columns.add(new ColumnConfig("codigo", constants.ventaCodigoPrenda(), 70));
		columns.add(new ColumnConfig("modelo", constants.ventaModeloPrenda(), 170));		
		
		ColumnConfig column = new ColumnConfig("cantidad", constants.ventaCantidad(), 70);
        column.setAlignment(HorizontalAlignment.RIGHT);        
        columns.add(column);
        
        ColumnModel cm = new ColumnModel(columns);
        
        AggregationRowConfig<BeanModel> total = new AggregationRowConfig<BeanModel>();
        total.setHtml("modelo", constants.ventaTotalResumen());        
        total.setSummaryType("cantidad", SummaryType.SUM);         
        total.setSummaryFormat("cantidad", NumberFormat.getFormat("0"));        
        cm.addAggregationRow(total);
        
        detalle = new Grid<BeanModel>(Graficos.storePrendasVendidasxDia, cm);
        detalle.addPlugin(r);
        GridSelectionModel<BeanModel> sm = new GridSelectionModel<BeanModel>();
        sm.setSelectionMode(SelectionMode.SINGLE);
        detalle.setSelectionModel(sm);
        
        detalle.setLoadMask(true);  
        detalle.setBorders(true);
        
        add(detalle, southData);
        
        buscar();
	}
	
	private boolean validate() {
		boolean des = fecha.isValid();
		boolean tien = tienda.isValid();
		return des && tien;
	}
	
	private void buscar(){
		status.show();
		find.hide();
		Graficos.fecha = fecha.getValue();
		
		if(tienda.getValue()!= null)
			Graficos.tienda_id = Integer.parseInt(tienda.getValue().get("id").toString());
		else
			Graficos.tienda_id = null;
		Graficos.pvxDia = singleton;
		Graficos.loaderPrendasVendidasxDia.load();	
	}
	
	public void setPermisos(List<String> permisos){
		boolean todas = permisos.contains("lastiendas");
		boolean su = permisos.contains("sutienda");
		
		if(todas){
			tiendaL.setVisible(true);
			tienda.setEnabled(true);
		    tienda.setVisible(true);		    
		}else if(su){
			tiendaL.setVisible(false);
			tienda.setEnabled(false);
		    tienda.setVisible(false);
		}
	}
	
//	private ChartModel getChartModel() {
//		ChartModel chartModel = new ChartModel("Population of Western European Countries in 1950 (000's)","fontsize:14px;color:#000000");
//		chartModel.setBackgroundColour("#ffffff");
//		XAxis xAxis = new XAxis();
//		xAxis.addLabels("Austria", "Belgium", "France", "Germany",
//				"Luxembourg", "Netherlands", "Switzerland");
//		chartModel.setXAxis(xAxis);
//		YAxis yAxis = new YAxis();
//		yAxis.setRange(0, 70000, 10000);
//		chartModel.setYAxis(yAxis);
//		
//		//BarChart chartConfig = new BarChart();
//		//BarChart chartConfig = new BarChart(BarStyle.THREED);
//		//BarChart chartConfig = new BarChart(BarStyle.GLASS);
//		//BarChart chartConfig = new CylinderBarChart();
//		
//		FilledBarChart chartConfig = new FilledBarChart();
//		chartConfig.setColour("#cc0000");
//		chartConfig.setOutlineColour("#660000");
//		
//		BarChart chartConfig = new SketchBarChart();
//		chartConfig.addBars(new BarChart.Bar(6936, "#FF0000"));
//		chartConfig.addBars(new BarChart.Bar(8628, "#FFA500"));
//		chartConfig.addBars(new BarChart.Bar(41832, "#FFFF00"));
//		chartConfig.addBars(new BarChart.Bar(68376,"#008000"));
//		chartConfig.addBars(new BarChart.Bar(296, "#0000FF"));
//		chartConfig.addBars(new BarChart.Bar(10114,"#4B0082"));
//		chartConfig.addBars(new BarChart.Bar(4693, "#EE82EE"));
//		
//		YAxis yAxis = new YAxis();
//		yAxis.addLabels("Switzerland",
//		"Netherlands","Luxembourg","Germany","France","Belgium","Austria");
//		yAxis.setOffset(true);
//		chartModel.setYAxis(yAxis);
//		XAxis xAxis = new XAxis();
//		xAxis.setRange(0, 70000, 10000);
//		chartModel.setXAxis(xAxis);
//		HorizontalBarChart chartConfig = new HorizontalBarChart();
//		
//		
//		PieChart chartConfig = new PieChart();
//		chartConfig.setColours("#FF0000", "#FFA500", "#FFFF00", "#008000",
//		"#0000FF","#4B0082", "#EE82EE");
//		
//		//chartConfig.addValues(6936,8628,41832,68376,296,10114,4693);
//		
//		chartConfig.addSlices(new PieChart.Slice(6936,"Austria"));
//		chartConfig.addSlices(new PieChart.Slice(8628,"Belgium"));
//		chartConfig.addSlices(new PieChart.Slice(41832,"France"));
//		chartConfig.addSlices(new PieChart.Slice(68376,"Germany"));
//		chartConfig.addSlices(new PieChart.Slice(296,"Luxembourg"));
//		chartConfig.addSlices(new PieChart.Slice(10114,"Netherlands"));
//		chartConfig.addSlices(new PieChart.Slice(4693,"Switzerland"));
//		
//		XAxis xAxis = new XAxis();
//		xAxis.addLabels("1950","1960","1970","1980","1990","2000");
//		chartModel.setXAxis(xAxis);
//		YAxis yAxis = new YAxis();
//		yAxis.setRange(40000, 90000, 10000);
//		yAxis.setOffset(true);
//		chartModel.setYAxis(yAxis);
//		LineChart chartConfig = new LineChart();
//		chartConfig.addValues(68376,72815,78169,78289,79433,82075);
//		chartConfig.setText("Germany");
//		
//		chartModel.addChartConfig(chartConfig);*/
//		//LineChart germanyChartConfig = new LineChart();
//		//BarChart germanyChartConfig = new BarChart();
//		AreaChart germanyChartConfig = new AreaChart();
//		germanyChartConfig.addValues(68376,72815,78169,78289,79433,82075);
//		germanyChartConfig.setColour("#ff0000");
//		germanyChartConfig.setText("Germany");
//		chartModel.addChartConfig(germanyChartConfig);
//		
//		//LineChart franceChartConfig = new LineChart();
//		AreaChart franceChartConfig = new AreaChart();
//		franceChartConfig.addValues(41832,45674,50771,53950,56842,59128);
//		franceChartConfig.setColour("#000066");
//		franceChartConfig.setText("France");
//		chartModel.addChartConfig(franceChartConfig);
//		
//		return chartModel;
//	}
}
